package com.hyperplan.neptune.parser;

import java.util.List;

import com.hyperplan.neptune.lexer.Token;

public class TokenParser implements Parser<Token> {

	private Class<?> cls;
	
	public TokenParser(Class<?> cls) {
		this.cls = cls;
	}
	
	public String getPatternName() {
		return String.format("Token [%s]", cls.getSimpleName());
	}
	
	public ParserSolution<Token> parse(List<Token> tokens, int index)
			throws ParserException {
		if (index < tokens.size()) {
			Token token = tokens.get(index);
			if (cls.isInstance(token)) {
				return new ParserSolution<Token>(token, index + 1);
			} else {
				throw new ParserException(getPatternName(), token);
			}
		} else {
			throw new ParserException(getPatternName());
		}
	}
}
